package com.ruibang.glass.material.service.impl.cp;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.common.domain.camunda.BpmProcessInstanceCreate;
import com.ruibang.glass.common.domain.camunda.BpmProcessInstanceStatus;
import com.ruibang.glass.common.domain.file.FileBusiness;
import com.ruibang.glass.common.domain.sale.SaleOrderManagement;
import com.ruibang.glass.common.utils.ApproveUserNameUtils;
import com.ruibang.glass.material.constant.Constants;
import com.ruibang.glass.material.constant.MaterialErrorCode;
import com.ruibang.glass.material.constant.MaterialStatusCode;
import com.ruibang.glass.material.constant.MaterialWareHouseCode;
import com.ruibang.glass.material.domain.req.bcp.BcpProductsOutReq;
import com.ruibang.glass.material.domain.req.bcp.ManageBCPOutInReq;
import com.ruibang.glass.material.dto.BCPOutAndInManagementDTO;
import com.ruibang.glass.material.entity.*;
import com.ruibang.glass.material.entity.cp.MaterialCPProductsOut;
import com.ruibang.glass.material.feign.CamundaApi;
import com.ruibang.glass.material.feign.FileApi;
import com.ruibang.glass.material.feign.SaleApi;
import com.ruibang.glass.material.mapper.GMaterialLineSideWarehouseLocationMapper;
import com.ruibang.glass.material.mapper.MaterialCpBcpItemsMapper;
import com.ruibang.glass.material.mapper.MaterialWarehouseInventoryCheckMapper;
import com.ruibang.glass.material.mapper.cp.MaterialCPProductsOutMapper;
import com.ruibang.glass.material.service.GMaterialALLInventoryManagementService;
import com.ruibang.glass.material.service.MaterialWarehouseInfoService;
import com.ruibang.glass.material.service.cp.MaterialCPProductsOutService;
import com.ruibang.glass.material.service.impl.CommonService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.exception.ServiceException;
import com.teaming.cloud.framework2.common.model.ResultBody;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 半成品出库详情 服务实现类
 * </p>
 *
 * @author mameng
 * @since 2024-04-02
 */
@Service
public class GMaterialCPProductsOutServiceImpl extends ServiceImpl<MaterialCPProductsOutMapper, MaterialCPProductsOut> implements MaterialCPProductsOutService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private CamundaApi camundaApi;

    @Autowired
    private RedisSingleService redisService;

    @Autowired
    private FileApi fileApi;

    @Autowired
    private GMaterialALLInventoryManagementService materialALLInventoryManagementService;

    private final String CPCKSQMENUID = "w0235dokc6a011a6f2b5840cea000038";

    @Autowired
    private MaterialCpBcpItemsMapper materialCpBcpItemsMapper;

    @Autowired
    private MaterialWarehouseInventoryCheckMapper materialWarehouseInventoryCheckMapper;

    @Autowired
    private GMaterialLineSideWarehouseLocationMapper materialLineSideWarehouseLocationMapper;

    @Autowired
    private MaterialWarehouseInfoService materialWarehouseInfoService;

    @Autowired
    private SaleApi saleApi;

    @Autowired
    private TransactionTemplate transactionTemplate;



    /**
     * 列表查询
     *
     * @param bcpProductsOutReq
     * @return
     */
    @Override
    public IPage<MaterialCPProductsOut> getPage(BcpProductsOutReq bcpProductsOutReq) {

        LambdaQueryWrapper<MaterialCPProductsOut> queryWrapper = Wrappers.<MaterialCPProductsOut>lambdaQuery();
        if (StringUtils.isNotBlank(bcpProductsOutReq.getOutStorageOrderNo())) {
            queryWrapper.like(MaterialCPProductsOut::getOutStorageOrderNo, bcpProductsOutReq.getOutStorageOrderNo());
        }
        if (StringUtils.isNotBlank(bcpProductsOutReq.getApproveStatus())) {
            queryWrapper.eq(MaterialCPProductsOut::getApproveStatus, bcpProductsOutReq.getApproveStatus());
        }
        if (StringUtils.isNotBlank(bcpProductsOutReq.getType())) {
            queryWrapper.eq(MaterialCPProductsOut::getType, bcpProductsOutReq.getType());
        }
        if (StringUtils.isNotBlank(bcpProductsOutReq.getPreparer())) {
            queryWrapper.eq(MaterialCPProductsOut::getPreparer, bcpProductsOutReq.getPreparer());
        }
        if (bcpProductsOutReq.getFinishStartTime() != null) {
            queryWrapper.ge(MaterialCPProductsOut::getFinishTime, bcpProductsOutReq.getFinishStartTime());
        }
        if (bcpProductsOutReq.getFinishEndTime() != null) {
            queryWrapper.le(MaterialCPProductsOut::getFinishTime, bcpProductsOutReq.getFinishEndTime());
        }
        Page<MaterialCPProductsOut> materialBcpProductsOutPage = baseMapper.selectPage(new Page<>(bcpProductsOutReq.getPageNo(), bcpProductsOutReq.getPageSize()),
                queryWrapper.orderByDesc(MaterialCPProductsOut::getCreateTime)
        );
        if (CollectionUtils.isEmpty(materialBcpProductsOutPage.getRecords())) {
            return materialBcpProductsOutPage;
        }
        Map<Object, Object> userMap = redisService.hmget(RedisKey.USER_GROUP_TYPE);
        List<String> businessKeys = materialBcpProductsOutPage.getRecords().stream().map(MaterialCPProductsOut::getId).collect(Collectors.toList());
        Map<String, Integer> statusMap = null;
        Map<String, String> processMap = null;
        Map<String, String> approveStatusMap = null;
        Map<String, String> userIdMap = null;
        Map<String, String> nodeName = null;
        //获取业务流程状态
        ResultBody<List<BpmProcessInstanceStatus>> resultBody = camundaApi.queryProcessInstanceStatus(businessKeys);
        if (resultBody != null && CollectionUtils.isNotEmpty(resultBody.getData())) {
            statusMap = resultBody.getData().stream().collect(Collectors.toMap(BpmProcessInstanceStatus::getBusinessKey, BpmProcessInstanceStatus::getStatus, (s1, s2) -> s1));
            processMap = resultBody.getData().stream().collect(Collectors.toMap(BpmProcessInstanceStatus::getBusinessKey, BpmProcessInstanceStatus::getProcessInstanceId, (s1, s2) -> s1));
            approveStatusMap = resultBody.getData().stream().collect(Collectors.toMap(BpmProcessInstanceStatus::getBusinessKey, BpmProcessInstanceStatus::getResult, (s1, s2) -> s1));
            userIdMap = resultBody.getData()
                    .stream()
                    .filter(status -> status.getUserId() != null)
                    .collect(Collectors.toMap(
                            BpmProcessInstanceStatus::getBusinessKey,
                            BpmProcessInstanceStatus::getUserId,
                            (s1, s2) -> s1
                    ));
            nodeName = resultBody.getData().stream().collect(Collectors.toMap(BpmProcessInstanceStatus::getBusinessKey, BpmProcessInstanceStatus::getNodeName, (s1, s2) -> s1));

        }
        for (MaterialCPProductsOut materialCPProductsOut : materialBcpProductsOutPage.getRecords()) {
            if (StringUtils.isNotEmpty(materialCPProductsOut.getWareHousePerson())) {
                if (!StringUtils.equals(materialCPProductsOut.getWareHousePerson(), "null")) {
                    String approveName = ApproveUserNameUtils.getApproveName(materialCPProductsOut.getWareHousePerson(), userMap);
                    materialCPProductsOut.setWareHousePerson(approveName);
                }
            }
            if (StringUtils.isNotEmpty(materialCPProductsOut.getWareHouseAdmin())) {
                if (!StringUtils.equals(materialCPProductsOut.getWareHouseAdmin(), "null")) {
                    String approveName = ApproveUserNameUtils.getApproveName(materialCPProductsOut.getWareHouseAdmin(), userMap);
                    materialCPProductsOut.setWareHouseAdmin(approveName);
                }
            }

            List<MaterialCpBcpItems> materialCpBcpItemsList = materialCpBcpItemsMapper.selectList(Wrappers.<MaterialCpBcpItems>lambdaQuery().select(MaterialCpBcpItems::getWarehouseName, MaterialCpBcpItems::getId).eq(MaterialCpBcpItems::getCpBcpId, materialCPProductsOut.getId()));
            String warehouseString = "";
            StringBuilder sb = new StringBuilder(); // 使用StringBuilder来构建字符串
            for (MaterialCpBcpItems materialCpBcpItems : materialCpBcpItemsList) {
                if (StringUtils.isNotEmpty(materialCpBcpItems.getWarehouseName())) {
                    if (sb.length() > 0) {
                        // 如果StringBuilder已经有内容了，就添加一个逗号
                        sb.append(",");
                    }
                    // 添加仓库名
                    sb.append(materialCpBcpItems.getWarehouseName());
                }
            }
            warehouseString = sb.toString();
            materialCPProductsOut.setWarehouseName(warehouseString);
            if (statusMap != null) {
                if (statusMap.get(materialCPProductsOut.getId()) != null) {
                    materialCPProductsOut.setStatus(statusMap.get(materialCPProductsOut.getId()).toString());
                }

            }
            if (processMap != null) {
                materialCPProductsOut.setProcessId(processMap.get(materialCPProductsOut.getId()));
            }
//            if (approveStatusMap != null) {
//                materialCPProductsOut.setApproveStatus(approveStatusMap.get(materialCPProductsOut.getId()));
//            }
            if (nodeName != null) {
                materialCPProductsOut.setNodeName(nodeName.get(materialCPProductsOut.getId()));
            }
            if (approveStatusMap != null) {
                String userId = userIdMap.get(materialCPProductsOut.getId());
                if (StringUtils.isNotBlank(userId)) {
                    String approveName = ApproveUserNameUtils.getApproveName(userId, userMap);
                    materialCPProductsOut.setApproveUser(approveName);
                    materialCPProductsOut.setApproveUserId(userId);
                }
            }
            materialCPProductsOut.setWarehouseCode(MaterialWareHouseCode.CPK_CK.getMsg());
        }
        return materialBcpProductsOutPage;

    }

    @Override
    public IPage<BCPOutAndInManagementDTO> getPage(ManageBCPOutInReq manageBCPOutInReq) {
        return baseMapper.getPage(new Page<>(manageBCPOutInReq.getPageNo(), manageBCPOutInReq.getPageSize()), manageBCPOutInReq);
    }

    /**
     * 新增流程
     *
     * @param materialCPProductsOut
     * @return
     */
    @Override
    public void addMaterialCPProductsOut(MaterialCPProductsOut materialCPProductsOut) {
        String execute = transactionTemplate.execute(tran -> {
            baseMapper.insert(materialCPProductsOut);
            if (CollectionUtils.isNotEmpty(materialCPProductsOut.getFileIds())) {
                FileBusiness fileBusiness = new FileBusiness();
                fileBusiness.setFileIds(materialCPProductsOut.getFileIds());
                fileBusiness.setBusinessId(materialCPProductsOut.getId());
                fileApi.updateBusinessById(fileBusiness);
            }
            if (StringUtils.isEmpty(materialCPProductsOut.getMenuId())) {
                materialCPProductsOut.setMenuId(CPCKSQMENUID);
            }
            if (CollectionUtils.isNotEmpty(materialCPProductsOut.getMaterialCpBcpItemsList())) {
                for (MaterialCpBcpItems materialCpBcpItems : materialCPProductsOut.getMaterialCpBcpItemsList()) {
                    if (materialCpBcpItems.getQuantity() == null && materialCpBcpItems.getLotQuantity() != null) {
                        materialCpBcpItems.setQuantity(materialCpBcpItems.getLotQuantity().intValue());
                    }
                    if (StringUtils.isNotEmpty(materialCpBcpItems.getWarehouseId()) && StringUtils.isEmpty(materialCpBcpItems.getWarehouseName())) {
                        MaterialWarehouseInfo byId = materialWarehouseInfoService.getById(materialCpBcpItems.getWarehouseId());
                        materialCpBcpItems.setWarehouseName(byId.getWarehouseName());
                    }
                    materialCpBcpItems.setCpBcpId(materialCPProductsOut.getId());
                    materialCpBcpItemsMapper.insert(materialCpBcpItems);
                }
            }
            return Constants.ONE_STR;
        });
        if (StringUtils.equals(execute, Constants.ONE_STR)) {

            if (!StringUtils.equals(materialCPProductsOut.getTrueOrFalseFormSale(), Constants.TWO_STR)) {
                BpmProcessInstanceCreate instanceCreate = new BpmProcessInstanceCreate();
                instanceCreate.setBusinessKey(materialCPProductsOut.getId());
                instanceCreate.setMenuId(materialCPProductsOut.getMenuId());
                //发起流程
                if (StringUtils.isNotBlank(materialCPProductsOut.getPreparerId())) {
                    Map<String, Object> variables = new HashMap<>();
                    variables.put(com.ruibang.glass.common.constants.Constants.PROCESS_INSTANCE_STARTER_USER_ID, materialCPProductsOut.getPreparerId());
                    variables.put(com.ruibang.glass.common.constants.Constants.PROCESS_INSTANCE_BUSINESS_KEY, materialCPProductsOut.getId());
                    variables.put(com.ruibang.glass.common.constants.Constants.PROCESS_INSTANCE_MENU_ID, materialCPProductsOut.getMenuId());
                    instanceCreate.setVariables(variables);
                }
                commonService.startProcess(instanceCreate);
            } else {
                for (MaterialCpBcpItems materialCpBcpItems : materialCPProductsOut.getMaterialCpBcpItemsList()) {
                    MaterialALLInventoryManagement one = materialALLInventoryManagementService.getOne(Wrappers.<MaterialALLInventoryManagement>lambdaQuery()
                            .eq(MaterialALLInventoryManagement::getStorageLocation, materialCpBcpItems.getStorageLocationId())
                            .eq(MaterialALLInventoryManagement::getATypeShelf, materialCpBcpItems.getATypeShelf())
                            .gt(MaterialALLInventoryManagement::getStockQuantity, Constants.ZERO_INT)
                            .last("limit 1")
                    );
                    if (one != null) {
                        one.setFrozenQuantity(BigDecimal.valueOf(one.getStockQuantity()));
                        materialALLInventoryManagementService.updateById(one);
                    }
                }
            }
        }
    }

    /**
     * 计算库存
     *
     * @param materialBcpProductsOut
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateItemHouse(MaterialCPProductsOut materialBcpProductsOut) {
        // 校验是否正在盘点
        Long aLong = materialWarehouseInventoryCheckMapper.selectCount(Wrappers.<MaterialWarehouseInventoryCheck>lambdaQuery()
                .eq(MaterialWarehouseInventoryCheck::getWarehouseCode, MaterialWareHouseCode.CPK_CK.getMsg())
                .eq(MaterialWarehouseInventoryCheck::getStatus, Constants.ONE_STR)
        );
        if (aLong > 0) {
            throw new ServiceException(MaterialErrorCode.CHECK_WAREHOUSE_OUT.getCode(), MaterialErrorCode.CHECK_WAREHOUSE_OUT.getMsg());
        }
        List<MaterialCpBcpItems> materialCpBcpItemsList = materialCpBcpItemsMapper.selectList(Wrappers.<MaterialCpBcpItems>lambdaQuery().eq(MaterialCpBcpItems::getCpBcpId, materialBcpProductsOut.getId()));

        for (MaterialCpBcpItems materialCpBcpItems : materialCpBcpItemsList) {
            if (StringUtils.isBlank(materialCpBcpItems.getATypeShelf())) {
                throw new ServiceException(MaterialStatusCode.NO_A_TYPE_LIST_NUMBER.getCode(), MaterialStatusCode.NO_A_TYPE_LIST_NUMBER.getMsg());
            }
        }

        BigDecimal bigDecimal = BigDecimal.ZERO;
        MaterialCPProductsOut materialBcpProductsOut1 = baseMapper.selectById(materialBcpProductsOut.getId());
        for (MaterialCpBcpItems materialCpBcpItems : materialCpBcpItemsList) {
            if (StringUtils.isBlank(materialCpBcpItems.getATypeShelf())) {
                throw new ServiceException(MaterialStatusCode.NO_A_TYPE_NUMBER.getCode(), MaterialStatusCode.NO_A_TYPE_NUMBER.getMsg());
            }
            MaterialALLInventoryManagement one = materialALLInventoryManagementService.getOne(Wrappers.<MaterialALLInventoryManagement>lambdaQuery()
                    .eq(MaterialALLInventoryManagement::getATypeShelf, materialCpBcpItems.getATypeShelf())
                    .eq(MaterialALLInventoryManagement::getWareHouseCode, MaterialWareHouseCode.CPK_CK.getMsg())
                    .ge(MaterialALLInventoryManagement::getStockQuantity, Constants.ONE_INT)
                    .orderByDesc(MaterialALLInventoryManagement::getStockQuantity)
                    .last("limit 1")
            );
            if (one == null) {
                throw new ServiceException(MaterialStatusCode.OVER_STATUS_IS_EMPTY.getCode(), MaterialStatusCode.OVER_STATUS_IS_EMPTY.getMsg());
            }
            bigDecimal = bigDecimal.add(BigDecimal.valueOf(one.getStockQuantity()));
            one.setStockQuantity(Constants.ZERO_INT);
            materialALLInventoryManagementService.updateById(one);
        }

        if (StringUtils.isNotEmpty(materialBcpProductsOut1.getOrderNumber())) {
            SaleOrderManagement saleOrderManagement = new SaleOrderManagement();
            saleOrderManagement.setOrderNumber(materialBcpProductsOut1.getOrderNumber());
            saleOrderManagement.setDelivered(bigDecimal);
            saleApi.updateGSaleOrderManagementByNumber(saleOrderManagement);
        }

        if (StringUtils.isNotEmpty(materialBcpProductsOut1.getShppingId())) {
            SaleOrderManagement saleOrderManagement = new SaleOrderManagement();
            saleOrderManagement.setId(materialBcpProductsOut1.getShppingId());
            saleOrderManagement.setShippedQuantity(bigDecimal);
            saleApi.saleShippingApplicationById(saleOrderManagement);
        }

        materialBcpProductsOut1.setFinishTime(LocalDateTime.now());
        materialBcpProductsOut1.setOutStatus(Constants.TWO_STR);
        baseMapper.updateById(materialBcpProductsOut1);
    }

    @Override
    @Transactional
    public void updateMaterialCpProductsInInfoList(MaterialCPProductsOut materialCPProductsOut) {
        MaterialCPProductsOut materialCPProductsOut1 = baseMapper.selectById(materialCPProductsOut.getId());
       List<MaterialCpBcpItems> materialCpBcpItemsList= materialCpBcpItemsMapper.selectList(Wrappers.<MaterialCpBcpItems>lambdaQuery().eq(MaterialCpBcpItems::getCpBcpId,materialCPProductsOut.getId()));
        //先解冻在冻结
        for (MaterialCpBcpItems materialCpBcpItems : materialCpBcpItemsList) {
            MaterialALLInventoryManagement one = materialALLInventoryManagementService.getOne(Wrappers.<MaterialALLInventoryManagement>lambdaQuery()
                    .eq(MaterialALLInventoryManagement::getStorageLocation, materialCpBcpItems.getStorageLocationId())
                    .eq(MaterialALLInventoryManagement::getATypeShelf, materialCpBcpItems.getATypeShelf())
                    .gt(MaterialALLInventoryManagement::getStockQuantity, Constants.ZERO_INT)
                    .last("limit 1")
            );
            if (one != null) {
                one.setFrozenQuantity(null);
                materialALLInventoryManagementService.updateById(one);
            }
        }

        for (MaterialCpBcpItems materialCpBcpItems : materialCPProductsOut.getMaterialCpBcpItemsList()) {
            MaterialALLInventoryManagement one = materialALLInventoryManagementService.getOne(Wrappers.<MaterialALLInventoryManagement>lambdaQuery()
                    .eq(MaterialALLInventoryManagement::getStorageLocation, materialCpBcpItems.getStorageLocationId())
                    .eq(MaterialALLInventoryManagement::getATypeShelf, materialCpBcpItems.getATypeShelf())
                    .gt(MaterialALLInventoryManagement::getStockQuantity, Constants.ZERO_INT)
                    .last("limit 1")
            );
            if (one != null) {
                one.setFrozenQuantity(BigDecimal.valueOf(one.getStockQuantity()));
                materialALLInventoryManagementService.updateById(one);
            }
        }
        fileApi.deleteByBusinessId(materialCPProductsOut.getId());
        if (CollectionUtils.isNotEmpty(materialCPProductsOut.getFileIds())) {
            FileBusiness fileBusiness = new FileBusiness();
            fileBusiness.setFileIds(materialCPProductsOut.getFileIds());
            fileBusiness.setBusinessId(materialCPProductsOut.getId());
            fileApi.updateBusinessById(fileBusiness);
        }
        if (CollectionUtils.isNotEmpty(materialCPProductsOut.getMaterialCpBcpItemsList())) {
            for (MaterialCpBcpItems materialCpBcpItems : materialCPProductsOut.getMaterialCpBcpItemsList()) {
                materialCpBcpItemsMapper.updateById(materialCpBcpItems);
            }
        }
        materialCPProductsOut.setApproveStatus(materialCPProductsOut1.getApproveStatus());
        baseMapper.updateById(materialCPProductsOut);
    }

    @Override
    public MaterialCPProductsOut getMaterialMaterialCPProductsOutById(String id) {
        MaterialCPProductsOut materialCPProductsOut = baseMapper.selectById(id);
        ResultBody<List<String>> fileIdsByBusinessId = fileApi.getFileIdsByBusinessId(id);
        if (CollectionUtils.isNotEmpty(fileIdsByBusinessId.getData())) {
            materialCPProductsOut.setFileIds(fileIdsByBusinessId.getData());
        }
        List<MaterialCpBcpItems> materialCpBcpItems = materialCpBcpItemsMapper.selectList(Wrappers.<MaterialCpBcpItems>lambdaQuery().eq(MaterialCpBcpItems::getCpBcpId, id));
        for (MaterialCpBcpItems materialCpBcpItems1 : materialCpBcpItems) {
            if (StringUtils.isNotEmpty(materialCpBcpItems1.getStorageLocationId()) && StringUtils.isEmpty(materialCpBcpItems1.getStorageLocationName())) {
                MaterialLineSideWarehouseLocation materialLineSideWarehouseLocation = materialLineSideWarehouseLocationMapper.selectById(materialCpBcpItems1.getStorageLocationId());
                materialCpBcpItems1.setStorageLocationName(materialLineSideWarehouseLocation.getLocationName());
            }
        }
        materialCPProductsOut.setMaterialCpBcpItemsList(materialCpBcpItems);
        return materialCPProductsOut;
    }

    @Override
    public void processNotice(String id) {
        MaterialCPProductsOut materialCPProductsOut = baseMapper.selectById(id);
        if (materialCPProductsOut.getApproveStatus().equals(Constants.FOUR_STR)) {
            List<MaterialCpBcpItems> materialCpBcpItemsList = materialCpBcpItemsMapper.selectList(Wrappers.<MaterialCpBcpItems>lambdaQuery().eq(MaterialCpBcpItems::getCpBcpId, materialCPProductsOut.getId()));
            for (MaterialCpBcpItems materialCpBcpItems : materialCpBcpItemsList) {
                MaterialALLInventoryManagement one = materialALLInventoryManagementService.getOne(Wrappers.<MaterialALLInventoryManagement>lambdaQuery()
                        .eq(MaterialALLInventoryManagement::getATypeShelf, materialCpBcpItems.getATypeShelf())
                        .eq(MaterialALLInventoryManagement::getWareHouseCode, MaterialWareHouseCode.CPK_CK.getMsg())
                        .ge(MaterialALLInventoryManagement::getStockQuantity, Constants.ONE_INT)
                        .orderByDesc(MaterialALLInventoryManagement::getStockQuantity)
                        .last("limit 1")
                );
                if (one != null) {
                    one.setFrozenQuantity(null);
                    materialALLInventoryManagementService.updateById(one);
                }
            }
            materialCPProductsOut.setFinishTime(LocalDateTime.now());
            baseMapper.updateById(materialCPProductsOut);
            //驳回时
            if (StringUtils.isNotEmpty(materialCPProductsOut.getShppingId())) {
                saleApi.updateSaleQualityById(materialCPProductsOut.getShppingId());
            }
        }

    }


    public String getApproveStatus(String id) {
        List<String> ids = new ArrayList<>();
        ids.add(id);
        ResultBody<List<BpmProcessInstanceStatus>> resultBody = camundaApi.queryProcessInstanceStatus(ids);
        Map<String, String> approveStatusMap = null;
        if (resultBody != null && CollectionUtils.isNotEmpty(resultBody.getData())) {
            approveStatusMap = resultBody.getData().stream().collect(Collectors.toMap(BpmProcessInstanceStatus::getBusinessKey, BpmProcessInstanceStatus::getResult, (s1, s2) -> s1));
        }
        if (approveStatusMap.get(id) != null) {
            return approveStatusMap.get(id);
        }
        return "";
    }


}
